//
//  NextTableViewController.m
//  UI考试_团购应用
//
//  Created by lanou3g on 15-3-23.
//  Copyright (c) 2015年 胡航. All rights reserved.
//

#import "NextTableViewController.h"
#import "NextModel.h"
#import "SNNTableViewController.h"
@interface NextTableViewController ()<NSXMLParserDelegate>

@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,copy)NSString *currentElementName;
@property(nonatomic,strong)NSMutableString *appendString;

@end

@implementation NextTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置标题
    self.title = @"所有分类";
    //设置左按钮
    [self leftBarButton];
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //调用解析方法
    [self xmlSaxAction];
    
   
}

-(void)xmlSaxAction
{
    NSString *domString = [[NSBundle mainBundle]pathForResource:@"Categories" ofType:@"xml"];

    NSData *data = [[NSData alloc]initWithContentsOfFile:domString];
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data];
    //设置代理
    xmlParser.delegate = self;
    //开始解析
    [xmlParser parse];
    
}
//1.开始解析文档
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.array = [NSMutableArray array];
}
//2.开始解析标签
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    //拼接字符串初始化
    self.appendString = [NSMutableString string];
    //记录当前标签
    self.currentElementName = elementName;
    
    if ([elementName isEqualToString:@"categories"]) {
        
        NextModel *model = [[NextModel alloc]init];
        model.subArray = [NSMutableArray array];
        model.catarray = [NSMutableArray array];
        [self.array addObject:model];
    }
}

//3.取标签中的值
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    //拼接字符串
    [self.appendString appendString:string];
    
}

//4.结束标签解析
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NextModel *temp = [self.array lastObject];
    
    if ([_currentElementName isEqualToString:@"category_name"]) {
        [temp.catarray addObject:self.appendString];
    }else if([_currentElementName isEqualToString:@"subcategories"])
    {
        [temp.subArray addObject:self.appendString];
    }
    
    self.currentElementName = nil;
    self.appendString = nil;

}

-(void)leftBarButton
{
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;

}

-(void)leftButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return self.array.count;
}

//更改区头字体大小
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
//    UITextField *headerTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 10, 200, 40)];
//    NextModel *n = self.array[section];
//
//    headerTextField.text = n.catarray[0];
//    headerTextField.font = [UIFont systemFontOfSize:40];
//    [header addSubview:headerTextField];
//    return header;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NextModel *m = self.array[section];
    
    return m.subArray.count;
}

//设置标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NextModel *n = self.array[section];

    return n.catarray[0];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NextModel *nextM = self.array[indexPath.section];
    cell.textLabel.text = nextM.subArray[indexPath.row];
    
    cell.textLabel.font = [UIFont systemFontOfSize:20];
   
    return cell;
}


//设置标题的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

//设置cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NextModel *m = self.array[indexPath.section];
    
    [_delegate backValue:m.subArray[indexPath.row]];
  
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
